package com.by.service.impl.impl;

import com.by.bean.ForwarderInquirySheet;
import com.by.bean.ForwarderProduct;
import com.by.bean.ForwarderQuoteList;
import com.by.bean.util.SheetType;
import com.by.common.bean.PageBean;
import com.by.common.crud.BaseServiceImpl;
import com.by.common.utils.Assert;
import com.by.common.utils.DateUtils;
import com.by.mapper.ForwarderInquirySheetMapper;
import com.by.mapper.ForwarderProductMapper;
import com.by.mapper.ForwarderQuoteListMapper;
import com.by.mapper.UserMapper;
import com.by.query.ForwarderQuery;
import com.by.query.FreightInquiryQuery;
import com.by.service.impl.ForwarderQuoteListService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class ForwarderQuoteListServiceImpl extends BaseServiceImpl<ForwarderQuoteList, ForwarderQuoteListMapper>implements ForwarderQuoteListService {
    @Autowired
    private ForwarderProductMapper forwarderProductMapper;
    @Autowired
    private ForwarderInquirySheetMapper forwarderInquirySheetMapper;
    @Autowired
     private  ForwarderQuoteListMapper forwarderQuoteListMapper ;
    /**
     * 当前最大编码
     */
    String maxCode;

    @Transactional
    @Override
    public int save(ForwarderQuoteList entity) {
        /*// 设置产品信息中的询价编码
        List<ForwarderProduct> products = entity.getProducts();
        if (products != null && !products.isEmpty()) {
            List<ForwarderProduct> newProducts = new ArrayList<>();
            for (ForwarderProduct product : products) {
                // 设置询价编码
                product.setQuote_coding(entity.getQuote_coding());
                // 使用有参构造创建新的ForwarderProduct对象
                ForwarderProduct newProduct = new ForwarderProduct(
                        entity.getQuote_coding(),
                        product.getProduct_number(),
                        product.getProduct_name(),
                        product.getPieces(),
                        product.getGross_weight(),
                        product.getVolume(),
                        product.getSupport_number(),
                        entity.getCustomer_id()
                );
                newProducts.add(newProduct);
            }
            // 批量保存产品信息
            int rs = forwarderProductMapper.saveBatch(newProducts);
            Assert.isZero(rs, "保存产品信息失败");
        }

        // 生成订单编码，订单编码 = 客户id + 四位随机数
        Random random = new Random();
        int fourDigitNumber = random.nextInt(9000) + 1000;
        String order_sn = entity.getCustomer_id() + "" + fourDigitNumber;

        ForwarderInquirySheet sheet = new ForwarderInquirySheet(null, entity.getQuote_coding(), order_sn, SheetType.DBJ, "先占着这个字段，后面再填", entity.getQuote_data());
        int rse = forwarderInquirySheetMapper.save(sheet);
        Assert.isZero(rse, "保存询价单失败");*/
        //生成编码
        String quote_coding =generateNewCode();
        // 生成订单编码，订单编码 = 客户id + 四位随机数
        String ym= DateUtils.getSystemTimeYM();
        Random random = new Random();
        int fourDigitNumber = random.nextInt(9000) + 1000;
        String order_sn =ym+fourDigitNumber;
        //先保存询价单
        int rs = forwarderInquirySheetMapper.save(quote_coding,order_sn);
        Assert.isZero(rs, "保存询价单失败");

        //设置询价清单（list）中的编码
        entity.setQuote_coding(quote_coding);
        //保存清单
        int rse = super.save(entity);
        Assert.isZero(rse, "保存询价清单失败");

        //保存产品信息
        if (entity.getProducts() != null && !entity.getProducts().isEmpty()) {
            int i = forwarderProductMapper.saveBatch(quote_coding,entity.getProducts());
            Assert.isZero(i, "保存产品信息失败");
        }
        return 1;
    }

    /**
     * 生成新的编码
     *
     * @return
     */
    String generateNewCode() {

        //获取系统日期
        String ym= DateUtils.getSystemTimeYM();
        //1. 判断 当前内存中是否有最大编码
        if(null==maxCode){
            //如果没有  查询最大编码
            maxCode=forwarderQuoteListMapper.queryMaxCode();
            if(null==maxCode){
                //说明当月还没有产生过货代 询价
                maxCode="HDXJ"+ym+"0001";
                return maxCode;
            }
        }

        //获取编码 中的月份 2503
        String codeYm=maxCode.substring(4,8);
        if(!ym.equals(codeYm)){
            //说明当月第一笔
            //说明当月还没有产生过货代 询价
            maxCode="HDXJ"+ym+"0001";
            return maxCode;
        }

        //累加
        //数据库存在当月最大编码   直接累加  HDXJ25030001
        String prefix=maxCode.substring(0,8);
        String seq=maxCode.substring(8);
        int tmp= Integer.parseInt(seq);
        tmp++;
        maxCode=prefix+String.format("%04d",tmp);
        return maxCode;

    }

}





